package com.loveazure.ui;

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.EventHandler;
import java.io.File;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import antlr.debug.Event;

import com.loveazure.bll.Handler;
import com.loveazure.eo.Course;
import com.loveazure.util.FileUtil;

public class CourseImportDialog extends JDialog {

	private JTextField courseNameField;
	private JTextField filePathField;
	private JButton selectFileBtn;
	private JButton okBtn;
	private JButton cancelBtn;
	private JFileChooser chooser;
	private File file;
	private Course course;
	private JProgressBar bar;

	public CourseImportDialog(JFrame owner, boolean modal) {
		super(owner, modal);

		setTitle("注册课程");
		setSize(300, 200);
		setLocationRelativeTo(null);
		GridBagLayout gbl = new GridBagLayout();
		gbl.columnWidths = new int[] { 40, 140, 60 };
		gbl.rowHeights = new int[] { 40, 40, 40,40 };
		setLayout(gbl);

		courseNameField = new JTextField();
		filePathField = new JTextField();
		selectFileBtn = new JButton("选择");
		selectFileBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				getFileChooser().showOpenDialog(CourseImportDialog.this);
				file = getFileChooser().getSelectedFile();
				if (file != null && file.exists()) {
					filePathField.setText(file.getPath());
				}
			}
		});
		okBtn = new JButton("确定");
		okBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (file == null || !file.exists()
						|| courseNameField.getText().trim() == "") {
					return;
				}
				bar.setVisible(true);
				Thread t = new ImportThread(file, courseNameField.getText(),
						bar.getModel(), CourseImportDialog.this);
				t.start();

			}
		});
		cancelBtn = new JButton("取消");
		cancelBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

		add(new JLabel("名称："), new GBC(0, 0, 1, 1));
		add(courseNameField, new GBC(1, 0, 2, 1).setFill(GBC.HORIZONTAL));
		add(new JLabel("路径："), new GBC(0, 1, 1, 1));
		add(filePathField, new GBC(1, 1, 1, 1).setFill(GBC.HORIZONTAL));
		add(selectFileBtn, new GBC(2, 1, 1, 1));
		add(okBtn, new GBC(1, 2, 1, 1));
		add(cancelBtn, new GBC(2, 2, 1, 1));
		
		bar = new JProgressBar();
		bar.setVisible(false);
		add(bar,new GBC(0, 3, 3, 1).setFill(GBC.HORIZONTAL));

	}

	private JFileChooser getFileChooser() {
		if (chooser == null) {
			chooser = new JFileChooser(".");
			chooser.setFileFilter(new FileNameExtensionFilter(
					"text file(*.txt)", "txt"));
		}
		return chooser;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}
}
